package com.copa.service.impl;

import com.copa.entity.IHttpMessage;
import com.copa.utils.MyPrivateMessageUtil;
import com.copa.service.PrivateService;
import org.springframework.stereotype.Service;

import static com.copa.constant.MessageType.*;

/**
 * @author copa
 * @createDate 2022-03-01 21:27
 * @function
 */
@Service
public class PrivateServiceImpl implements PrivateService {

    @Override
    public void sendMessage(IHttpMessage message) {
        switch (message.getType()) {
            case TEXT_MESSAGE:
                sendTextMessage(message);
                break;
            case PIC_MESSAGE:
                sendPicMessage(message);
                break;
            case VOICE_MESSAGE:
                sendVoiceMessage(message);
                break;
            case BUSINESS_CARD_MESSAGE:
                sendBusinessCardMessage(message);
                break;
            case VIDEO_MESSAGE:
                sendVideoMessage(message);
                break;
            case FACE_MESSAGE:
                sendFaceMessage(message);
                break;
            case POSITION_MESSAGE:
                sendPositionMessage(message);
                break;
            case LINK_MESSAGE:
                sendLinkMessage(message);
                break;
            case TRANSFER_MESSAGE:
                sendTransferMessage(message);
                break;
            case RED_PACKAGE_MESSAGE:
                sendRedPackageMessage(message);
                break;
            case MINI_PROGRAM_MESSAGE:
                sendMiniProgramMessage(message);
                break;
            case GROUP_INVITE_MESSAGE:
                sendGroupInviteMessage(message);
                break;
        }
    }

    private void sendMiniProgramMessage(IHttpMessage message) {
        String msg = message.getMsg();
        String title = msg.substring(msg.indexOf("<title>") + 7, msg.indexOf("</title>"));
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFinal_from_wxid(), "哦？要推 " + title + " 小程序给我吖~");


    }

    private void sendLinkMessage(IHttpMessage message) {
        String msg = message.getMsg();
        if (msg.contains("<title>")) {
            String title = msg.substring(msg.indexOf("<title>") + 7, msg.indexOf("</title>"));
            String appName = msg.substring(msg.indexOf("<appname>") + 9, msg.indexOf("</appname>"));
            MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFinal_from_wxid(), "哦？要推 " + appName + " 的 " + title + " 给我吖~");
            return;
        }
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFinal_from_wxid(), "哦？要发 " + msg + " 文件给我吖~但是我不会看的吖~");
    }

    private void sendVideoMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFinal_from_wxid(), "要发视频给我看吗？但是我不会看啦~");
    }

    private void sendBusinessCardMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFinal_from_wxid(), "要推荐给我吗？但是我不会看啦~");
    }

    private void sendVoiceMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFinal_from_wxid(), "好好听耶~但是我不会说话啦~");
    }

    private void sendTransferMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFinal_from_wxid(), "谢谢你的好意~但我是不收款哒~");

    }

    private void sendGroupInviteMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFrom_wxid(), "感谢你拉我入群~但我是不进群哒~");
    }

    private void sendRedPackageMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFrom_wxid(), "谢谢你的好意~但我是不收红包哒~");
    }

    private void sendPositionMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFrom_wxid(),
                "哦？原来你在 " + message.getMsg() + " 吖~");
    }

    private void sendFaceMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFrom_wxid(), "秘技：学口学舌");
        MyPrivateMessageUtil.sendFaceMessage(message.getTo_wxid(), message.getFrom_wxid(), message.getMsg());
    }

    /**
     * 回复图片消息
     * @param message
     */
    private void sendPicMessage(IHttpMessage message) {
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFrom_wxid(), "秘技：学口学舌");
        MyPrivateMessageUtil.sendPicMessage(message.getTo_wxid(), message.getFrom_wxid(), message.getMsg());
    }

    /**
     * 回复文本消息
     * @param message
     */
    private void sendTextMessage(IHttpMessage message) {
        String msg = message.getMsg();
        // 音乐
        if (msg.startsWith("音乐 ")) {
            msg = msg.replace("音乐 ", "");
            String[] split = msg.split("#");
            if (split.length >= 2) {
                MyPrivateMessageUtil.sendMusicMessage(message.getTo_wxid(), message.getFrom_wxid(), split[0], "酷狗".equals(split[1]) ? 1 : 2);
                return;
            }
            MyPrivateMessageUtil.sendMusicMessage(message.getTo_wxid(), message.getFrom_wxid(), msg, 0);
            return;
        }
        MyPrivateMessageUtil.sendTextMessage(message.getTo_wxid(), message.getFrom_wxid(), "指令输入错误喵！不鸟你啦~");
    }

}
